using Microsoft.Phone.Controls.Maps.AutomationPeers;
using Microsoft.Phone.Controls.Maps.Core;
using System;
using System.Windows;
using System.Windows.Automation.Peers;
using System.Windows.Controls;
using System.Windows.Media;
namespace Microsoft.Phone.Controls.Maps
{
	public class MapItemsControl : ItemsControl, IProjectable
	{
		private ItemsPresenter ip;
		private MapLayer ml;
		public MapBase ParentMap
		{
			get
			{
				IProjectable projectable = base.get_Parent() as IProjectable;
				if (projectable != null)
				{
					return projectable.ParentMap;
				}
				return base.get_Parent() as MapBase;
			}
		}
		public MapItemsControl()
		{
			base.set_DefaultStyleKey(typeof(MapItemsControl));
		}
		public void ProjectionUpdated(ProjectionUpdateLevel updateLevel)
		{
			if (updateLevel != ProjectionUpdateLevel.None)
			{
				base.InvalidateArrange();
				base.InvalidateMeasure();
			}
		}
		public override void OnApplyTemplate()
		{
			base.OnApplyTemplate();
			if (VisualTreeHelper.GetChildrenCount(this) > 0)
			{
				this.ip = (VisualTreeHelper.GetChild(this, 0) as ItemsPresenter);
				if (this.ip != null)
				{
					this.ip.add_LayoutUpdated(new EventHandler(this.ip_LayoutUpdated));
				}
			}
		}
		protected override Size ArrangeOverride(Size finalSize)
		{
			if (this.ml != null)
			{
				this.ml.InvalidateArrange();
			}
			return base.ArrangeOverride(finalSize);
		}
		protected override Size MeasureOverride(Size availableSize)
		{
			if (this.ml != null)
			{
				this.ml.InvalidateMeasure();
			}
			return base.MeasureOverride(availableSize);
		}
		private void ip_LayoutUpdated(object sender, EventArgs e)
		{
			this.ip.remove_LayoutUpdated(new EventHandler(this.ip_LayoutUpdated));
			if (VisualTreeHelper.GetChildrenCount(this.ip) > 0)
			{
				this.ml = (VisualTreeHelper.GetChild(this.ip, 0) as MapLayer);
			}
		}
		protected override AutomationPeer OnCreateAutomationPeer()
		{
			return new BaseAutomationPeer(this, "MapItemsControl");
		}
	}
}
